package com.example.blog.controller.user;

import com.example.blog.pojo.LoginRequest;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.User;
import com.example.blog.pojo.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.util.PcUtils.HeaderFileUtils;
import com.example.blog.util.TokenUtil.GetCookie;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;

    //登录接口
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest, HttpServletResponse response) {
        return userService.login(loginRequest, response);
    }

    //扫码登录接口
    @PostMapping("/QRCodeLogin/{userId}")
    Result<String> QRCodeLogin(@PathVariable Integer userId, HttpServletResponse response) {
        return userService.QRCodeLogin(userId, response);
    }

    //退出登录接口
    @GetMapping("/outLogin")
    public Result outLogin(HttpServletRequest request, HttpServletResponse response) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        try {
            if (token != null) {
                return userService.outLogin(token, response);
            }
        } catch (Exception e) {
            return Result.failed("退出失败！：" + e.getMessage());
        }
        return Result.failed("请先登录！");
    }

    //查询登录用户数
    @GetMapping("/tokenCount")
    public Result tokenCount() {
        return userService.tokenCount();
    }

    //注册接口
    @PostMapping("/register")
    public Result register(User user, HttpServletRequest request) throws ServletException, IOException {
        Part HeaderfilePart = request.getPart("avatar"); // 获取上传文件
        if (HeaderfilePart != null) {
            HeaderFileUtils.HeaderResult result = HeaderFileUtils.savePart(request, HeaderfilePart);
            user.setHeaderurl(result.getUrl());
        }

        //新增用户信息
        return userService.register(user);
    }

    //根据用户id查用户
    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    //查询当前用户
    @GetMapping("/queryCurrentUserInfo")
    public Result<UserInfo> queryCurrentUserInfo(HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        //新增用户信息
        return userService.queryCurrentUserInfo(token);
    }

    //查询所有用户信息
    @GetMapping("/getAllUsers")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    //修改用户信息
    @PutMapping("/update/{id}")
    public Result updateUserRecord(@RequestBody User user, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return userService.updateUserRecord(user, token);
    }

    //删除用户信息
    @DeleteMapping("/delete/{id}")
    public Result deleteUserRecord(@PathVariable int id, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return userService.deleteUserRecord(id, token);
    }

}
